package com.agent.service.impl;

import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.agent.entity.Dept;
import com.agent.entity.RepaireInfo;
import com.agent.page.EasyuiPage;
import com.agent.service.IDeptService;
import com.agent.util.Constants;
import com.agent.vo.RepaireAndSatisVO;
import com.agent.vo.RepaireInfoVO;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.agent.repository.CustomerSatisfactionMapper;
import com.agent.entity.CustomerSatisfaction;
import com.agent.service.ICustomerSatisfactionService;


@Service("customerSatisfactionService")
@Transactional
public class CustomerSatisfactionServiceImpl implements ICustomerSatisfactionService{
	
	@Autowired
	protected CustomerSatisfactionMapper customerSatisfactionMapper;

	@Autowired
	IDeptService deptService;

	public void delete(String id) {
		customerSatisfactionMapper.delete(id);
		
	}

	@Override
	public EasyuiPage getRepaireAndSatis(Integer page, Integer rows, RepaireAndSatisVO repaireAndSatisVO) {
		List<Dept> deptList = deptService.listNoPage(new Dept());
		PageHelper.startPage(page, rows,true, true, true);
		List<RepaireAndSatisVO> list = customerSatisfactionMapper.getRepaireAndSatis(repaireAndSatisVO);
		list.forEach(vo -> {
			vo.setCallerDeptName(coverToName(vo.getCallerDeptId(),deptList));
			vo.setAddress(Constants.getAddress(vo.getAddress()));
			vo.setSource(Constants.getSourceMap(vo.getSource()));
		});
		EasyuiPage<RepaireAndSatisVO> pages = new EasyuiPage<RepaireAndSatisVO>();
		pages.setRows(list);
		PageInfo<RepaireAndSatisVO> pageinfo = new PageInfo<RepaireAndSatisVO>(list);
		pages.setTotal(pageinfo.getTotal());
		return pages;
	}

	@Override
	public Workbook exportExcel(RepaireAndSatisVO repaireAndSatisVO) throws Exception {
		List<Dept> deptList = deptService.listNoPage(new Dept());
		List<RepaireAndSatisVO> list = customerSatisfactionMapper.getRepaireAndSatis(repaireAndSatisVO);
		list.forEach(vo -> {
			vo.setCallerDeptName(coverToName(vo.getCallerDeptId(),deptList));
			vo.setAddress(Constants.getAddress(vo.getAddress()));
			vo.setSource(Constants.getSourceMap(vo.getSource()));
		});
		return ExcelExportUtil.exportExcel(new ExportParams("满意度调查", "满意度调查"),RepaireAndSatisVO.class,list);
	}

	public String coverToName(String deptId,List<Dept> deptList){
		final String[] deptName = {""};
		deptList.forEach(dept -> {
			if(dept.getQywxId().equals(deptId)){
				deptName[0] = dept.getDeptName();
				return;
			}
		});
		return deptName[0];
	}
	public List<CustomerSatisfaction> getCustomerSatisfactionList(String id) {
		return customerSatisfactionMapper.getCustomerSatisfaction(id);
	}
	
	public CustomerSatisfaction getCustomerSatisfaction(String id) {
		List<CustomerSatisfaction> list = customerSatisfactionMapper.getCustomerSatisfaction(id);
		return list==null||list.size()==0l?null:list.get(0);
	}

	public void insert(CustomerSatisfaction customerSatisfaction)throws Exception {
		 customerSatisfactionMapper.insert(customerSatisfaction);
	}

	public List<CustomerSatisfaction> searchAll() {
		return customerSatisfactionMapper.searchAll();
	}

	public PageInfo<CustomerSatisfaction> searchCustomerSatisfaction(Map searchMap) {
		CustomerSatisfaction customerSatisfaction = (CustomerSatisfaction) searchMap.get("customerSatisfaction");
		int pageSize = Integer.parseInt(searchMap.get("pageSize").toString());
		int pageNo = Integer.parseInt(searchMap.get("pageNo").toString());
		PageHelper.startPage(pageNo, pageSize,true, true, true);
		List<CustomerSatisfaction> list = customerSatisfactionMapper.searchCustomerSatisfaction(customerSatisfaction);
		PageInfo<CustomerSatisfaction> pageinfo = new PageInfo<CustomerSatisfaction>(list);
		return pageinfo;
	}

	public void update(CustomerSatisfaction CustomerSatisfaction) {
		customerSatisfactionMapper.update(CustomerSatisfaction);
	}

	public CustomerSatisfactionMapper getCustomerSatisfactionMapper() {
		return customerSatisfactionMapper;
	}

	public void setCustomerSatisfactionMapper(CustomerSatisfactionMapper CustomerSatisfactionMapper) {
		this.customerSatisfactionMapper = CustomerSatisfactionMapper;
	}
	
	
	

}
